package control;

import java.util.ArrayList;

import debug.Console;

public class WriteRight {
	private JavaSpeech javaspeech;
	private TokenizeText tokenize;
	private Semantic semantic;
	private Syntatic syntatic;
	private String text;
	
	public WriteRight() {
		try {
			javaspeech = new JavaSpeech();
		} catch (Exception e) {
			Console.addErrorLine("Java Speech : " + e.getMessage());
			e.printStackTrace();
		}
		tokenize = new TokenizeText("");
	}
	
	public void setText(String _text) {
		text = _text;
		tokenize.setText(_text);
	}
	
	public ArrayList<WordInfo> tokenizeText() {
		
		ArrayList<WordInfo> ret = null;
		try {
			ret = tokenize.tokenize();
			javaspeech.say("Your text has been tokenized");
		} catch (Exception e) {
			javaspeech.say("Tokenize Error, " + e.getMessage());
		}
		
		return ret;
	}
	
	public ArrayList<WordInfo> getTokens() {
		return tokenize.getTokens();
	}
	
	public boolean checkSyntaxe() {
		syntatic = new Syntatic(getTokens());
		try {
			if (syntatic.checkSentence()) {
				javaspeech.say("Your text is gammaticaly correct");
			} else {
				javaspeech.say("Your text has grammatical problems");
				return false;
			}
		} catch (Exception e) {
			Console.addErrorLine("Syntatic Error : " + e.getMessage());
			javaspeech.say(e.getMessage());
			return false;
		}
		return true;
	}
	
	public boolean checkSemantic() {
		semantic = new Semantic(syntatic.getSentence());
		try {
			if (semantic.checkTypes()) {
				javaspeech.say("Your text is correct");
				return true;
			}
		} catch (Exception e) {
			Console.addErrorLine(e.getMessage());
			javaspeech.say("Your text has problems, " + e.getMessage());
		}
		return false;
	}
	
	public void saySentence() {
		javaspeech.say(text);
	}
}
